package com.settlement.system.service.csv;

import com.settlement.system.service.IBaseService;
import com.settlement.system.common.util.common.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author author
 */
@Slf4j
public class DateSpiltCsvDatas extends BaseServiceCsvDatas {

    public DateSpiltCsvDatas(IBaseService baseService, Map<String, Object> params) {
        super(baseService, params);
    }

    @Override
    protected List<Map<String, Object>> getListParams(Map<String, Object> params) {

        Object startOutDate = params.get("startOutDate");
        Object endOutDate = params.get("endOutDate");

        try {
            if (startOutDate != null && endOutDate != null
                    && StringUtils.isNotEmpty(startOutDate.toString())
                    && StringUtils.isNotEmpty(endOutDate.toString())
                    && startOutDate.toString().compareTo(endOutDate.toString()) <= 0) {
                List<String> dates = DateUtil.findDates(startOutDate.toString(), endOutDate.toString());
                List<Map<String, Object>> mapList = new ArrayList<>();
                for (String date : dates) {
                    HashMap<String,Object> map = (HashMap<String, Object>) ((HashMap<String, Object>) params).clone();
                    map.put("startOutDate",date);
                    map.put("endOutDate",date);
                    mapList.add(map);
                }
                return mapList;
            }
        } catch (Exception ex) {
            log.warn("拆分异常,采用正常模式导出",ex);
        }


        return super.getListParams(params);
    }
}
